package org.elephas.webapp.frontend.jquery;

import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;

public abstract class SimpleModal extends Panel {
	private static final long serialVersionUID = 1L;

	private static final ResourceReference JS = new JavascriptResourceReference(
			SimpleModal.class, "res/jquery.simplemodal-1.1.1.pack.js");

	private static final ResourceReference CSS = new ResourceReference(
			SimpleModal.class, "res/simplemodal.css");

	private Panel content;

	public SimpleModal(String id) {
		super(id);
		add(new JQueryBehavior());
		add(HeaderContributor.forJavaScript(JS));
		add(HeaderContributor.forCss(CSS));

		content = (Panel) newPanel("content").setOutputMarkupId(true);
		add(content);
	}

	@Override
	protected void onComponentTag(ComponentTag tag) {
		super.onComponentTag(tag);
		tag.put("style", "display:none;");
	}

	protected abstract Panel newPanel(String id);

	public void show(AjaxRequestTarget target) {
		Component c = get("content");

		StringBuffer sb = new StringBuffer();
		sb.append("$('#").append(c.getMarkupId());
		sb
				.append("').modal({close: false,  "
						+ "onOpen: function (dialog) {  dialog.overlay.fadeIn('fast', function () {    dialog.container.slideDown('fast', function () {      dialog.data.fadeIn('fast');    });  }); }});");
		target.appendJavascript(sb.toString());
	}
}
